package com.tao.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.tao.gulimall.product.entity.SpuInfoEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.product.dao.SkuInfoDao;
import com.tao.gulimall.product.entity.SkuInfoEntity;
import com.tao.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
		this.baseMapper.insert(skuInfoEntity);
	}

	@Override
	public PageUtils queryPageByCondition(Map<String, Object> params) {
		LambdaQueryWrapper<SkuInfoEntity> lqw = new LambdaQueryWrapper<>();
		// 判断查询参数
		String key = (String) params.get("key");
		if(!StringUtils.isNullOrEmpty(key) ){
			lqw.and(item->{
				item.like(SkuInfoEntity::getSkuName, key).or().eq(SkuInfoEntity::getSkuId, key);
			});
		}
		String brandId = (String) params.get("brandId");
		if(!StringUtils.isNullOrEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
			lqw.eq(SkuInfoEntity::getBrandId, brandId);
		}
		String categoryId = (String) params.get("categoryId");
		if(!StringUtils.isNullOrEmpty(categoryId) && !"0".equalsIgnoreCase(categoryId)){
			lqw.eq(SkuInfoEntity::getCatalogId, categoryId);
		}

		// 价格区间
		String min = (String) params.get("min");
		if(!StringUtils.isNullOrEmpty(min)){
			lqw.ge(SkuInfoEntity::getPrice, min);
		}

		String max = (String) params.get("max");
		if(!StringUtils.isNullOrEmpty(max)){
			try{
				BigDecimal bigDecimal = new BigDecimal(max);
				if (bigDecimal.compareTo(new BigDecimal(0)) == 1){
					lqw.ge(SkuInfoEntity::getPrice, max);
				}
			}catch (Exception e){

			}
		}
		IPage<SkuInfoEntity> page = this.page(
			new Query<SkuInfoEntity>().getPage(params),
				lqw
		);

		return new PageUtils(page);


	}

}